#!/usr/bin/perl


use 5.010;
use warnings;
use strict;



my @field=([],[]);
my $coord;
my $c1;
my $c2;
my $c3;
my $c4;
my $bool=0;

sub field1
{
  for (my $i = 0; $i <=10; $i++)
     {
	  for (my $j = 0; $j <=10; $j++)
	  {	    
	      $field[$i][$j]=' / ';
	    
	  }
      }
}
sub printfield
{	
  say '';
  for (my $i = 0; $i <=10; $i++)
     {
	if ($i == 0) {
	    print " .  0  1  2  3  4  5  6  7  8  9 ";
	}
	else {
	  for (my $j = 0; $j <=10; $j++)
	  {
	    if ($j == 0) {
	      print " ",(($i-1))," ";
	    }
	    else { 	    
	      print "$field[$i][$j]";
	    }
	  }
	}
	say '';
      }
      say '';      
}

sub getcoord
{
    say 'If you want to exit enter 0';
    for (my $i = 1; $i <= 10; $i++)
     {  
	print "Enter coordinates of $i ship: ";
	$coord = <STDIN>;
	($c1,$c2,$c3,$c4)=split(//,$coord,4);
	
	if ($coord == 0) 
	{
		say 'Goodbye!';
		$i=10;
	}
	
	if ($coord=~(/^[0-9]{4}\n$/)) 
   	{
		my $len1;
		my $len2;
		$len1= (($c4-$c2+1));	  
		$len2= (($c3-$c1+1));

		if ($len1>4 || $len2>4 || ($c1 != $c3 && $c2 != $c4))
		{
			say 'The ship with incorrect coordinates will not be printed!';
			$i--;
		}
		else
		{	 
		  	if($c1 == $c3) 
			{ 	
				for(my $k=0;$k < $len1;$k++)
				{
					$field[$c1+1][$c2+$k+1]=' & ';      
				}
			}
		  	else
		  	{		  
				for(my $g=0;$g < $len2;$g++)
				{
					$field[$c1+1+$g][$c2+1]=' & ';
			  	}
			}	      
			&printfield;
		}
	}
  }
  say ''; 
}
  
    


&field1;
say "_____________________________________________";
say '';
&printfield;
&getcoord;
say "_____________________________________________";
say '';
